// pages/background/background.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    image: '',
    tempFilePaths: '',
    appid: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },




  // 时间戳  根据当前时间生成时间戳
  // md5加密  时间戳

  // aes base64
  onAdd: function (e) {
    let that = this
    const db = wx.cloud.database()

    wx.chooseImage({
      success: function (res) {
        var timestamp = '' + new Date().valueOf() + '.png';
        wx.cloud.uploadFile({
          cloudPath: timestamp,
          filePath: res.tempFilePaths[0], // 文件路径
          success: res => {
            // get resource ID
            that.data.image = res.fileID;
            wx.cloud.callFunction({
              name: "login",
              data: {},
              success: res => {
                let openid = res.result.openid;

                db.collection('user').where({
                  _openid: openid // 填入当前用户 openid
                }).get({
                  success: function (res) {

                    if (res.data.length > 0) {
                      that._update(that.data.image, res.data[0]._id);
                    } else {
                      that._add(that.data.image);
                    }
                  }
                })
              },

              fail: res => { }
            })
          }
        })
      },
    })
  },


  _add: function (image) {

    let that = this;
    //把图片存到users集合表
    const db = wx.cloud.database();
    db.collection('user').add({
      // data 字段表示需新增的 JSON 数据
      data: {
        image: that.data.image
      },
      success: function (res) {
        wx.showToast({
          title: "图片上传成功",
          icon: "none"
        })
        that.setData({
          image: image
        })
      },
      fail: console.error
    })

  },


  _update: function (image, openid) {

    let that = this;
    const db = wx.cloud.database();

    db.collection('user').doc(openid).update({
      // data 传入需要局部更新的数据
      data: {
        // 表示将 done 字段置为 true
        image: image
      },
      success: res => {
        that.setData({
          image: image
        })
      },
      fail: res => { },
    })
  },


  onReduction: function (e) {

    let that = this
    const db = wx.cloud.database()

    // get resource ID

    wx.cloud.callFunction({
      name: "login",
      data: {},
      success: res => {
        let openid = res.result.openid;
        db.collection('user').where({
          _openid: openid // 填入当前用户 openid
        }).get({
          success: function (res) {

            if (res.data.length > 0) {
              that._update('', res.data[0]._id);
            } else {
              that._add(that.data.image);
            }
            wx.showToast({
              title: "背景还原成功",
              icon: "none"
            })
            setTimeout(() => {
            wx.navigateBack({
              delta: 1,
            })
            }, 900 );
          }
        })
      },

      fail: res => { }
    })


  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})